package com.jft.spread.controller;


import com.jft.spread.context.UserContext;
import com.jft.spread.pojo.dos.UserDO;
import com.jft.spread.pojo.req.UserLoginReqVO;
import com.jft.spread.pojo.req.UserPasswordReqVO;
import com.jft.spread.pojo.req.UserReqVO;
import com.jft.spread.pojo.vo.AuthUser;
import com.jft.spread.pojo.vo.ResultMessage;
import com.jft.spread.service.UserLoginService;
import com.jft.spread.token.Token;
import com.jft.spread.utils.ResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/userLogin")
public class UserLoginController {

    @Resource
    private UserLoginService userLoginService;

    /**
     * 密码登录
     *
     * @param reqVO
     * @return
     */
    @PostMapping("/passwordLogin")
    public ResultMessage<Token> login(@RequestBody @Validated UserLoginReqVO reqVO) {
        Token token = userLoginService.passwordLogin(reqVO);
        return ResultUtil.data(token);
    }


    /**
     * 修改密码
     *
     * @param reqVO
     * @return
     */
    @PostMapping("/updatePassword")
    public ResultMessage<Token> updatePassword(@RequestBody @Validated UserPasswordReqVO reqVO) {
        Token token = userLoginService.updatePassword(reqVO);
        return ResultUtil.data(token);
    }

    /**
     * 获取我的信息
     *
     * @return
     */
    @PostMapping("/getUserDetails")
    public ResultMessage<AuthUser> getUserDetails() {
        return ResultUtil.data(UserContext.getCurrentUser());
    }

    /**
     * 刷新token
     */
    @PostMapping("/refreshToken")
    public ResultMessage<Token> refreshToken(@RequestHeader String accessToken) {
        Token token = userLoginService.refreshToken(accessToken);
        return ResultUtil.data(token);
    }

    /**
     * 退出登录
     */
    @PostMapping("/outLogin")
    public ResultMessage<Boolean> outLogin(@RequestHeader String accessToken) {
        userLoginService.outLogin(accessToken);
        return ResultUtil.success();
    }

    @PostMapping("/selectUser")
    public ResultMessage<List<UserDO>> selectUser(@RequestBody UserLoginReqVO userDO) {
        List<UserDO> userDOS = userLoginService.selectUser(userDO);
        return ResultUtil.data(userDOS);
    }

    @PostMapping("/selectUserOne")
    public ResultMessage<List<UserDO>> selectUserOne(@RequestBody UserLoginReqVO userDO) {
        List<UserDO> userDOS = userLoginService.selectUserOne(userDO);
        return ResultUtil.data(userDOS);
    }

    @PostMapping("/insertUserOne")
    public ResultMessage<Boolean> insertUserOne(@RequestBody @Validated UserReqVO userReqVO) {
        userLoginService.insertUserOne(userReqVO);
        return ResultUtil.success();
    }

    @PostMapping("/updateUser")
    public ResultMessage<List<UserDO>> updateUserOne(@RequestBody UserLoginReqVO userDO) {
        Token token = userLoginService.updatePassword(userDO);
        return ResultUtil.data(userDO);
    }
}
